package com.ljf.daily.softwareautorecord.domain.validator;

import com.ljf.common.domain.validator.BasisValidator;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.EnumUtil;
import com.ljf.config.domain.repository.ConfigDatRecordSymbolRepository;
import com.ljf.config.dto.query.ConfigDatRecordSymbolQuery;
import com.ljf.config.enums.ConfigDatRecordSymbolTypeEnum;
import com.ljf.daily.makemoneysoftware.domain.repository.DailyDatMakeMoneySoftwareRepository;
import com.ljf.daily.softwareautorecord.domain.repository.DailyDatSoftwareAutoRecordRepository;
import com.ljf.daily.softwareautorecord.dto.command.DailyDatSoftwareAutoRecordCommand;
import com.ljf.daily.makemoneysoftware.dto.query.DailyDatMakeMoneySoftwareQuery;
import com.ljf.daily.softwareautorecord.dto.query.DailyDatSoftwareAutoRecordQuery;
import com.ljf.daily.softwareautorecord.enums.DailyDatSoftwareAutoRecordAmountHistoryEnum;
import com.ljf.daily.softwareautorecord.enums.DailyDatSoftwareAutoRecordAutoExecutionEnum;
import com.ljf.daily.softwareautorecord.enums.DailyDatSoftwareAutoRecordModeTypeEnum;
import com.ljf.daily.softwareautorecord.model.DailyDatSoftwareAutoRecord;
import com.ljf.daily.softwareautorecord.vo.DailyDatSoftwareAutoRecordVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * 赚钱软件自动记录  业务校验
 *
 * @author LJF
 * @since 2024-12-14
 */
@Component
@AllArgsConstructor
public class DailyDatSoftwareAutoRecordValidator extends BasisValidator<DailyDatSoftwareAutoRecordQuery, DailyDatSoftwareAutoRecordCommand, DailyDatSoftwareAutoRecordVO, DailyDatSoftwareAutoRecord, DailyDatSoftwareAutoRecordRepository> {

    private final DailyDatMakeMoneySoftwareRepository makeMoneySoftwareRepository;
    private final ConfigDatRecordSymbolRepository configDatRecordSymbolRepository;

    @Override
    public void checkAdd(DailyDatSoftwareAutoRecordCommand command) throws ServiceException {
        super.checkAdd(command);
        this.checkRecordIntegrity(command);
    }

    /**
     * 数据完整性校验
     *
     * @param command
     * @throws ServiceException
     */
    private void checkRecordIntegrity(DailyDatSoftwareAutoRecordCommand command) throws ServiceException {
        if (!EnumUtil.checkCode(command.getModeType(), DailyDatSoftwareAutoRecordModeTypeEnum.values())) {
            throw new ServiceException("数据完整性校验异常: 模式类型异常");
        }
        if (!EnumUtil.checkCode(command.getAmountHistory(), DailyDatSoftwareAutoRecordAmountHistoryEnum.values())) {
            throw new ServiceException("数据完整性校验异常: 金额历史异常");
        }
        if (Objects.equals(DailyDatSoftwareAutoRecordModeTypeEnum.MONTH.getCode(), command.getModeType())) {
            if (command.getExecutionDate() == null) {
                throw new ServiceException("数据完整性校验异常: 执行日期不能为空");
            }
            if (String.valueOf(command.getExecutionDate()).length() > 2) {
                throw new ServiceException("数据完整性校验异常: 执行日期异常");
            }
        }
        if (Objects.equals(DailyDatSoftwareAutoRecordModeTypeEnum.YEAR.getCode(), command.getModeType())) {
            if (command.getExecutionDate() == null) {
                throw new ServiceException("数据完整性校验异常: 执行日期不能为空");
            }
            if (String.valueOf(command.getExecutionDate()).length() > 4 || String.valueOf(command.getExecutionDate()).length() < 3) {
                throw new ServiceException("数据完整性校验异常: 执行日期异常");
            }
        }
        if (!EnumUtil.checkCode(command.getAutoExecution(), DailyDatSoftwareAutoRecordAutoExecutionEnum.values())) {
            throw new ServiceException("数据完整性校验异常: 自动执行异常");
        }
        if (Objects.equals(DailyDatSoftwareAutoRecordAutoExecutionEnum.YES.getCode(), command.getAutoExecution())
                && (command.getWithdrawalCount() == null || command.getWithdrawalCount().compareTo(BigDecimal.ZERO) == 0)) {
            throw new ServiceException("数据完整性校验异常: 提现统计不能为空");
        }
        int count = makeMoneySoftwareRepository.count(
                DailyDatMakeMoneySoftwareQuery.builder().code(command.getSoftwareCode()).build()
        );
        if (count <= 0) {
            throw new ServiceException("数据完整性校验异常: 软件编码不存在");
        }
        count = configDatRecordSymbolRepository.count(
                ConfigDatRecordSymbolQuery.builder()
                        .type(ConfigDatRecordSymbolTypeEnum.DAILY_DAT_SOFTWARE_AUTO_RECORD.getCode())
                        .code(command.getSymbolCode())
                        .build()
        );
        if (count <= 0) {
            throw new ServiceException("数据完整性校验异常: 标签编码不存在");
        }
    }

    /**
     * 获取模板  业务校验
     *
     * @param date
     * @throws ServiceException
     */
    public void checkQueryTemplate(Date date) throws ServiceException {
        if (date == null) {
            throw new ServiceException("获取模板校验异常:日期不能为空");
        }
    }

}
